//
//  RHCellMarkerView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/6.
//

#import "RHCellMarkerView.h"
@interface RHCellMarkerView()<ChartMarker>

@property (nonatomic, assign) CGPoint offset;

@property (nonatomic, copy) NSString *maskerTag;
@end
@implementation RHCellMarkerView


#pragma mark -------- chartMarker delegate -------
- (CGPoint)offsetForDrawingAtPoint:(CGPoint)atPoint
{
    return CGPointMake(0, 0);
}
-(void)refreshContentWithEntry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight
{
//    NSLog(@"---->entry:%@",entry.yy_modelDescription);
//    NSLog(@"---->highlight:%@",highlight.yy_modelDescription);
    //位置
    if(highlight.dataIndex == self.chartModel.max_position){
        self.maskerTag = [NSString stringWithFormat:@"%@",self.chartModel.pointDataArr[3]];
    }
    if(highlight.dataIndex == self.chartModel.min_position){
        self.maskerTag = [NSString stringWithFormat:@"%@",self.chartModel.pointDataArr[4]];
    }
}

- (void)drawWithContext:(CGContextRef _Nonnull)context point:(CGPoint)point {
    CGFloat point_img_margin = 8;
    CGFloat text_img_margin = 3;
    NSString *markerImage = @"cell_volt_masker";
    if([self.chartModel.sectionTitle isEqualToString:@"温度统计".rh_localized]){
        markerImage = @"cell_temp_masker";
    }
//    NSLog(@"------->绘制:%@",NSStringFromCGPoint(point));
    CGFloat textW = [self.maskerTag getStringWidthFont:[UIFont systemFontOfSize:10] limitHeight:20];
    CGFloat textH = [self.maskerTag getStringHeightFont:[UIFont systemFontOfSize:10] limitWidth:100];
    
    CGFloat textX = point.x-textW/2;
    CGFloat textY = point.y-self.height/2-textH/2-point_img_margin-text_img_margin;
    
    CGFloat imageX = point.x-(self.width/2);
    CGFloat imageY = point.y-self.height-point_img_margin;
    
    if(point.y < self.height + point_img_margin ){
        markerImage = @"cell_volt_masker_up";
        if([self.chartModel.sectionTitle isEqualToString:@"温度统计".rh_localized]){
            markerImage = @"cell_temp_masker_up";
        }
        imageY = point.y + point_img_margin;
        textY = point.y + point_img_margin + self.height/2 - textH/2 + text_img_margin;
    }
    
    
    [[UIImage imageNamed:markerImage] drawInRect:CGRectMake(imageX, imageY, self.width, self.height)];
    [self.maskerTag drawAtPoint:CGPointMake(textX, textY) withAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor,NSFontAttributeName:[UIFont systemFontOfSize:10]}];
}





@end
